package com.warehouse.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.Action;
import com.warehouse.entity.Order;
import com.warehouse.service.OrderService;
import com.warehouse.utils.Page;
import com.warehouse.utils.ResultTemplate;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter

public class OrderAction {
    private OrderService orderService = new OrderService();
    private Order order;
    private Object obj;

    //添加商品
    public String add() {
        int add = orderService.add(order);
        if (add > 0) {
            obj = JSONArray.toJSON(new ResultTemplate(200, "添加成功", null));
        } else {
            obj = JSONArray.toJSON(new ResultTemplate(500, "添加失败", null));
        }
        return Action.SUCCESS;
    }

    //修改商品
    public String update() {
        int update = orderService.update(order);
        if (update > 0) {
            obj = JSONArray.toJSON(new ResultTemplate(200, "修改成功", null));
        } else {
            obj = JSONArray.toJSON(new ResultTemplate(500, "修改失败", null));
        }
        return Action.SUCCESS;
    }

    private Integer id;

    //删除管理员
    public String del() {
        int delete = orderService.delete(id);
        if (delete > 0) {
            obj = JSONArray.toJSON(new ResultTemplate(200, "删除成功", null));
        } else {
            obj = JSONArray.toJSON(new ResultTemplate(500, "删除失败", null));
        }
        return Action.SUCCESS;
    }

    //根据id查询内容
    public String byId() {
        Order order = orderService.byId(id);
        if (order != null) {
            obj = JSONArray.toJSON(new ResultTemplate(200, "获取成功", order));
        } else {
            obj = JSONArray.toJSON(new ResultTemplate(500, "获取失败", null));
        }
        return Action.SUCCESS;
    }

    //分页
    private Integer startPage;

    public String page() {
        Page<Order> adminPage = orderService.queryPage(startPage, Page.PAGE_SIZE);
        obj = JSONObject.toJSON(adminPage);
        return Action.SUCCESS;
    }


    //模糊查询加分页
    private Integer type;
    public String byStatus() {
        Page<Order> cargoPage = orderService.byTypeName(startPage, Page.PAGE_SIZE, type);
        obj = JSONObject.toJSON(cargoPage);
        return Action.SUCCESS;
    }

    public String out(){
        int update = orderService.updateOut(order);
        if (update > 0) {
            obj = JSONArray.toJSON(new ResultTemplate(200, "出库成功", null));
        } else {
            obj = JSONArray.toJSON(new ResultTemplate(500, "出库失败", null));
        }
        return Action.SUCCESS;
    }

}
